//
//  IMINetXML.m
//  IMINet
//
//  Created by Travis on 09-11-29.
//  Copyright imi.im 2009. All rights reserved.
//	More Information: http://imi.im

#import "IMINetXML.h"

@implementation IMINetXMLNode
@synthesize name,value,children,parent,attributes;
@synthesize xpath;
+(id)node{
	return [[[self alloc] init] autorelease];
}
- (id) init
{
	self = [super init];
	if (self != nil) {
		self.name=@"";
		self.value=[NSMutableString string];
		self.children=[NSMutableArray array];
		
	}
	return self;
}


-(void)addChild:(IMINetXMLNode*)child{
	if (![self.children containsObject:child]) {
		[self.children addObject:child];
		child.parent=self;
	}
	
}
-(void)removeChild:(IMINetXMLNode*)child{
	if (child!=nil && [self.children count]>0) {
		if([self.children containsObject:child]){
			[self.children removeObject:child];
		}
	}
}
- (NSString*)xpath{
	IMINetXMLNode *p=self.parent;
	
	if (p!=nil) {
		return [NSString stringWithFormat:@"%@/%@",p.xpath ,self.name];
	}
	
	return [@"/" stringByAppendingString:self.name];
}
@end


@implementation IMINetXML
@synthesize currentChild;

- (id) initWithData:(NSData*)xmlData withDelegate:(NSObject<IMINetXMLDelegate> *)dlg
{
	self = [super init];
	if (self != nil) {
		delegate=dlg;
		parser = [[NSXMLParser alloc] initWithData:xmlData];

        [parser setDelegate:self];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldResolveExternalEntities:NO];
        
        [parser parse];
	}
	return self;
}
- (void) dealloc
{
	[super dealloc];
}

@end

@implementation IMINetXML(NSXMLParserDelegate)

- (void)parserDidStartDocument:(NSXMLParser *)theParser
{
    if ([delegate respondsToSelector:@selector(XMLDidStart:) ]) {
		[delegate XMLDidStart:self];
		
	}
}


- (void)parserDidEndDocument:(NSXMLParser *)theParser
{
	NSLog(@"Parsing complete: %@", self.name);
    [theParser release];
	
	if ([delegate respondsToSelector:@selector(XMLDidFinish:) ]) {
		[delegate XMLDidFinish:self];
	}
}


- (void)parser:(NSXMLParser *)theParser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict
{
	if (currentChild==nil) {
		self.currentChild=self;
	}else {
		
		IMINetXMLNode *node=[[IMINetXMLNode alloc] init];
		[currentChild addChild:[node autorelease]];
		self.currentChild=node;
		
	}
	
	currentChild.name=elementName;
	currentChild.attributes=[NSMutableDictionary dictionaryWithDictionary:attributeDict];
	
	NSLog(@"%@",currentChild.xpath);
}


- (void)parser:(NSXMLParser *)theParser foundCharacters:(NSString *)characters
{
	
	[currentChild.value appendString:characters];
    
}


- (void)parser:(NSXMLParser *)theParser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	NSLog(@"%@\n<<<%@",currentChild.value, currentChild.name);
	self.currentChild=currentChild.parent;
	
}


- (void)parser:(NSXMLParser *)theParser foundAttributeDeclarationWithName:(NSString *)attributeName 
    forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue
{
    //NSLog(@"Found attribute: %@ (%@) [%@] {%@}", attributeName, elementName, type, defaultValue);
}


- (void)parser:(NSXMLParser *)theParser foundIgnorableWhitespace:(NSString *)whitespaceString
{
    NSLog(@"Found ignorable whitespace: %@", whitespaceString);
}


- (void)parser:(NSXMLParser *)theParser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"Parsing error occurred: %@", parseError);
	
}

@end
